package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.Vo.NursingPlanDto;
import com.zzyl.serve.Vo.NursingPlanVo;
import com.zzyl.serve.Vo.NursingProjectPlanVo;
import com.zzyl.serve.domain.*;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        //根据护理计划id查询所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo ;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional( rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        //拷贝属性
        BeanUtils.copyProperties(dto,nursingPlan);// 拷贝非空属性
        save(nursingPlan);//保存
        nursingPlan.setCreateTime(DateUtils.getNowDate());//设置创建时间
        nursingPlan.setUpdateTime(DateUtils.getNowDate());//设置更新时间
        //plan
        // 批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
        //add mid
        //get mid list
        //mapper add list
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional

    public int updateNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        //
//        List<NursingProjectPlan> list = dto.getProjectPlans();
        if(dto.getProjectPlans()!=null){
            //删除原有的护理计划
//            LambdaUpdateWrapper<NursingProjectPlan> objectLambdaUpdateWrapper = Wrappers.lambdaUpdate();
//            objectLambdaUpdateWrapper.eq(NursingProjectPlan::getPlanId, nursingPlan.getId());
//            nursingProjectPlanMapper.delete(objectLambdaUpdateWrapper);
//
//            log.warn("---------------------------------------------------------------" + dto.getProjectPlans());
            //把新的插入进去
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());;
        }
        return updateById(nursingPlan)?1:0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        nursingProjectPlanMapper.deleteByPlanId(id);
        return removeById(id) ? 1 : 0;
    }
}
